<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cart extends CI_Controller {
	
	public function view($page, $data = array())
	{
		$this->load->model('UserModel');
		$this->load->library(array('cart', 'table'));
		
		$this->load->view("templates/header");
		$this->load->view("templates/sidebars", array('logged_in' => $this->UserModel->isLoggedIn(), 'cart' => $this->cart->contents()));
		$this->load->view($page, $data);
		$this->load->view("templates/footer");
	}
	
	public function add()
	{
		$item_id = $this->uri->segment(3);
		
		$this->load->model('AdminModel');
		if(!$this->AdminModel->doesDVDExist($item_id))
		{
			$this->view('admin/admin_invalid_dvd_id');
			return;
		}
		
		$query = $this->db->get_where('dvds', array('dvd_id' => $item_id));
		$row = $query->row();
		
		$item_price = $row->dvd_price;
		$item_name = $row->dvd_title;
		
		$this->load->library('cart');
		$this->cart->insert(array(
			'id' => $item_id,
			'qty' => 1,
			'price' => $item_price,
			'name' => $item_name,
			'dvd_cover' => $row->dvd_cover
		));
	}
}